package com.company;

public class TestJoin {

        public static void main(String[] args) throws InterruptedException {

            Object object=new Object();
                       Thread thread1=new Thread(()->{
                synchronized (object){
                    System.out.println("线程1获得锁");
                    try {
                        System.out.println("线程1等待");
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("线程1执行完成");
            });

            Thread thread2=new Thread(()->{
                synchronized (object){
                    System.out.println("线程2获得锁");
                    try {
                        System.out.println("线程2等待");
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("线程2执行完成");
            });

            Thread thread3=new Thread(()->{
                synchronized (object){
                    System.out.println("线程3获得锁");
                    try {
                        System.out.println("线程3等待");
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("线程3执行完成");
            });
            thread1.start();
            thread2.start();
            thread3.start();

            Thread thread4=new Thread(()->{
                synchronized (object){
                    System.out.println("线程4获得锁");// 其它线程进入wait后 立即释放锁，所以这里可以获得锁
                    //使用一次notify只能唤醒一个线程；
                    //使用一次notifyAll可唤醒所有的线程
//                    object.notify();
                    object.notifyAll();
                    System.out.println("线程4去唤醒wait的线程");
                }

            });
            thread4.start();


        }

//    public static void main(String[] args) throws InterruptedException {
////        // TODO Auto-generated method stub
////        System.out.println(Thread.currentThread().getName()+" start");
////        ThreadTest t1=new ThreadTest("A");
////        ThreadTest t2=new ThreadTest("B");
////        ThreadTest t3=new ThreadTest("C");
////        System.out.println("t1start");
////        t1.start();
////        t1.join();
////        System.out.println("t1end");
////        System.out.println("t2start");
////        t2.start();
////        t2.join();
////        System.out.println("t2end");
////        System.out.println("t3start");
////        t3.start();
////        t3.join();
////        System.out.println("t3end");
////        System.out.println(Thread.currentThread().getName()+" end");
////    }
////
////
////}
////class ThreadTest extends Thread{
////    private String name;
////    public ThreadTest(String name){
////        this.name = name;
////    }
////    public void run(){
////        for(int i=1;i<=5;i++){
////            System.out.println(name+"-"+i);
////        }
////    }
}